//
//  TeamViewController.m
//  MeterMart
//
//  Created by ENERGY on 2018/10/23.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "TeamViewController.h"
#import "TeamTableViewCell.h"

@interface TeamViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)NSMutableArray * cellArr;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)UILabel * myLeaderContentLab;//我的上级
@property (nonatomic, strong)UILabel * oneLevelContentLab;//一级团队
@property (nonatomic, strong)UILabel * twoLevelContentLab;//二级团队
@property (nonatomic, strong)NSDictionary * headerModel;//头部模型
@property (nonatomic, assign)int currentPage;//第几页
@end

@implementation TeamViewController(networking)
- (void)getTeamData{
    NSDictionary * dic = @{
                           @"unionid":[UserModel sharedInstanced].userID,
                           @"typeid":self.teamType,
                           @"page":@(self.currentPage),
                           @"size":@"8"
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"teamApi/myTeam" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            if (self.currentPage == 1) {
                [self.cellArr removeAllObjects];
            }
            self.headerModel = responseObject[@"list"][@"myTeam"];
            [self.cellArr addObjectsFromArray:responseObject[@"list"][@"cusInfo"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

@end
@implementation TeamViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    self.currentPage = 1;
    [self getTeamData];//团队数据
}
#pragma mark - init
- (void)setHeaderModel:(NSDictionary *)headerModel{
    _headerModel = headerModel;
    self.myLeaderContentLab.text = headerModel[@"higher"];
    self.oneLevelContentLab.text = headerModel[@"oneTeam"];
    self.twoLevelContentLab.text = headerModel[@"twoTeam"];
}
//- (NSMutableArray *)cellArr{
//    if (!_cellArr) {
//        _cellArr = @[
//                     @{@"name":@"王二狗", @"num":@"XXX", @"status":@"已生效", @"iconImg":@""},
//                     @{@"name":@"李二蛋", @"num":@"XXX", @"status":@"未生效", @"iconImg":@""},
//                     @{@"name":@"赵铁柱", @"num":@"XXX", @"status":@"已生效", @"iconImg":@""},
//                     @{@"name":@"孙狗剩", @"num":@"XXX", @"status":@"已生效", @"iconImg":@""}
//                     ].mutableCopy;
//    }return _cellArr;
//}
- (NSMutableArray *)cellArr{
    if (!_cellArr) {
        _cellArr = @[].mutableCopy;
    }return _cellArr;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 113.5/WIDTH_6_SCALE;
        [_tableView registerClass:[TeamTableViewCell class] forCellReuseIdentifier:NSStringFromClass([TeamTableViewCell class])];
        _tableView.backgroundColor = getColor(mainColor);
        if ([self.navigationItem.title isEqualToString:@"我的团队"]) {
            _tableView.tableHeaderView = self.headerView;
        }else{
            _tableView.tableHeaderView = [UIView new];
        }
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.currentPage = 1;
            [self getTeamData];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.currentPage ++;
            [self getTeamData];
        }];
    }return _tableView;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100/WIDTH_6_SCALE)];
        _headerView.backgroundColor = getColor(mainColor);
        //白色背景
        UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(10/WIDTH_6_SCALE, 10/WIDTH_6_SCALE, SCREEN_WIDTH - 20/WIDTH_6_SCALE, 90/WIDTH_6_SCALE)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:whiteView];
        //标题
        for (int i = 0; i<3; i++) {
            UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(whiteView.width/3 * i, 0, whiteView.width/3, 47/WIDTH_6_SCALE)];
            titleLab.tag = 70000 + i;
            [self label:titleLab withFont:DEF_FontSize_12 withText:@"--" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:whiteView];
        }
        UILabel * myLeaderTitleLab = (UILabel *)[whiteView viewWithTag:70000];
        UILabel * oneLevelTitleLab =  (UILabel *)[whiteView viewWithTag:70001];
        UILabel * twoLevelTitleLab =  (UILabel *)[whiteView viewWithTag:70002];
        myLeaderTitleLab.text = @"我的上级";
        oneLevelTitleLab.text = @"一级团队(人)";
        twoLevelTitleLab.text = @"二级团队(人)";
        //分割线
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 47/WIDTH_6_SCALE, whiteView.width, 1/WIDTH_6_SCALE)];
        lineView.backgroundColor = getColor(mainColor);
        [whiteView addSubview:lineView];
        //内容
        for (int i = 0; i<3; i++) {
            UILabel * contentLab = [[UILabel alloc] initWithFrame:CGRectMake(whiteView.width/3 * i, 48/WIDTH_6_SCALE, whiteView.width/3, whiteView.height - 48/WIDTH_6_SCALE)];
            contentLab.tag = 80000 + i;
            [self label:contentLab withFont:DEF_FontSize_14 withText:@"--" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:whiteView];
        }
        self.myLeaderContentLab = (UILabel *)[whiteView viewWithTag:80000];
        self.oneLevelContentLab = (UILabel *)[whiteView viewWithTag:80001];
        self.twoLevelContentLab = (UILabel *)[whiteView viewWithTag:80002];
        self.myLeaderContentLab.text = @"--";
        self.myLeaderContentLab.textColor = [UIColor blackColor];
        self.oneLevelContentLab.text = @"--";
        self.oneLevelContentLab.textColor = getColor(mainRedColor);
        self.twoLevelContentLab.text = @"--";
        self.twoLevelContentLab.textColor = getColor(mainRedColor);
    }return _headerView;
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TeamTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TeamTableViewCell class])];
    cell.model = self.cellArr[indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cellArr.count;
}

@end
